#include <iostream>
#include "Meshes/Uniform.hpp"
using namespace Tuna;

typedef TunaArray<double,2>::huge ScalarField2D;

void readFromFile_DX(ScalarField2D& scf1, int number, string filename, int w); 
void writeToFile_Profile(ScalarField2D& scf, int number, string filename, 
		       double dx, int mony, int w); 

int main() 
{
  double length_x, length_y, dt; 
  int num_nodes_x, num_nodes_y, frequency;
  int Init, Nmax;
  int mony[3];

  std::ifstream input_cin ("inputProfiles"); 
  input_cin >> length_x >> length_y
	    >> num_nodes_x >> num_nodes_y
	    >> dt
	    >> Init
	    >> Nmax
	    >> frequency
	    >> mony[0]
	    >> mony[1]
	    >> mony[2];
  input_cin.close();
  
  StructuredMesh<Uniform<double, 2> > mesh(length_x, num_nodes_x,
					   length_y, num_nodes_y);
  double dx = mesh.getDelta(X), dy = mesh.getDelta(Y);
  ScalarField2D tn( mesh.getExtentNodes()   );


  ofstream temp_fp("./");
  double t = dt * Init;
  double Tmax = dt * Nmax;
  int iteration = Init;
  while (iteration <= Nmax) {
    std::cout << "\n ---- Iteration " << iteration ;
    readFromFile_DX(tn, iteration, "./temp.", 5);
    //    cout << pn; cin >> pausa;
    writeToFile_Profile(tn, iteration, "./tgn1.", dx, mony[0], 5);
    writeToFile_Profile(tn, iteration, "./tgn2.", dx, mony[1], 5);
    writeToFile_Profile(tn, iteration, "./tgn3.", dx, mony[2], 5);
    iteration += frequency;
  }
  
  return 0; 
}

void writeToFile_Profile(ScalarField2D& scf, int number, string filename, double dx, 
		       int mony, int w) 
{   
    ostringstream extnumber;
    extnumber.width(w);
    extnumber.fill('0');
    extnumber << number;
    string snumber = extnumber.str();
    filename += snumber;

    ofstream file (filename.c_str());
    if (!file) {
      cout << "\n !Can't open file \" " << filename << "\"" << endl;
    } else {
      cout << "\n Writing file : \" " << filename << "\"" << endl;
    }

    for(int i = 0; i <= scf.ubound(firstDim); ++i) {
      file << i * dx << "\t" << scf(i, mony) << endl;
      cout << i * dx << "\t" << scf(i, mony) << endl;
    }

    file.close();   
}


void readFromFile_DX(ScalarField2D& scf1, int number, string filename, int w) 
{   
    ostringstream extnumber;
    extnumber.width(w);
    extnumber.fill('0');
    extnumber << number;
    string snumber = extnumber.str();
    filename += snumber;
    ifstream file (filename.c_str());

    if (!file) {
      cout << "\n !Can't open file \" " << filename << "\"" << endl;
    } else {
      cout << "\n Reading file : \" " << filename << "\"" << endl;
    }

    char line[256];
/// 
/// by the time, just jump the first 7 lines
///	
    for(int ii = 1; ii <= 8; ++ii)
	file.getline(line,256);
    
    file.precision(10);
    file.setf(ios_base::fixed,ios_base::floatfield);
///
/// Here, scf1, scf2 and scf3 are supposed to be of type ScalarField2D
///
    for(int i = 0; i <= scf1.ubound(firstDim); ++i) 
      for(int j = 0; j <= scf1.ubound(secondDim); ++j)
	file >> scf1(i,j);

    file.close();
}
